#! /usr/bin/env node

// const program = require('commander')

// program.option('-t, --type [type]', 'this is a test')
//     .action((...arg)=>{
//         console.log(7, program.opts())
//     })

// const args02 = process.argv.slice(0, 2);
// // program.parse(args02.concat([]))
// console.log(process.argv)

const { Plop, run } = require('plop');
const path = require('path')

const args = process.argv.slice(2);
const argv = require('minimist')(args);



const qs = require('qs'),
    ws = require('ws'),
    socketServer = ws.Server;

let wss = new socketServer({ port: 8765 });

wss.on('connection', function (ws, info) {
    ws.on('message', function (e) {
        const data = JSON.parse(e)
        console.log('message', data)

        // program.parse(args02.concat(['-t', data.type]))

        global.$ws = ws

        Plop.launch({
            cwd: argv.cwd,
            configPath: path.join(__dirname, '../plopfile/test.js'),
            require: argv.require,
            completion: argv.completion,
        }, (env, ...arg) => {
                console.log(13, env)
                env.confirm = true

                return run(env, ...arg)
        })
        
    })

    ws.on('close', function () {
        console.log('关闭')
    })
})